// contactform.js
var contactFormNamespace = contactFormNamespace || {};

contactFormNamespace.onFormLoad = function (executionContext) {
    //debugger;
 
    let errorphone = contactFormNamespace.getLangString("phonemsg");
    
    var formContext = executionContext.getFormContext();
    formContext.getAttribute("telephone1").addOnChange(contactFormNamespace.telephoneOnChange);

    // 检查窗体是否为编辑状态
    if (formContext.ui.getFormType() === 2) { // 2 表示编辑状态
        //debugger
        // 获取电话字段的值
        var phoneNumber = formContext.getAttribute("telephone1").getValue();

        // 如果电话字段为空，提示用户
        if (!phoneNumber) {
            var phoneControl = formContext.getControl("telephone1");
            if (phoneControl) {
                phoneControl.setNotification(errorphone, "phoneWarning");
            }
        }
    }
}

contactFormNamespace.telephoneOnChange=function(executionContext){
    //debugger;
    var eventSource = executionContext.getEventSource();
    var phoneNumber = eventSource.getValue();
    let phoneControl = eventSource.controls.get(0)//formContext.getControl("telephone1");
    let errorphone = contactFormNamespace.getLangString("phonemsg");
    if(phoneNumber){
        phoneControl.clearNotification("phoneWarning");
    }else{
         phoneControl.setNotification(errorphone, "phoneWarning");
    }
}

contactFormNamespace.getLangString = function (key) {
    let lcid = Xrm.Utility.getGlobalContext().userSettings.languageId;
    let resourcePath = `qzw_/resxs/ss_lang_${lcid}`;
    return Xrm.Utility.getResourceString(resourcePath, key);
}